//#include <atltime.h>
#include "uti_db.h"
//#include "uti_consolecolor.h"
//#include "uti_timer.h"

#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/format.hpp>
using namespace std;
using namespace boost::filesystem;


////////////////////////global main.cpp////////////////////////////

extern char gTradingDay[15];
///////////////////////////////////////////////////////////////////

//
//DB::DB(BarInfo* pbar, BarSeries_broad** ppbarserbroad, unsigned int cnt)
//{
//	memcpy(&m_barinfo, pbar, sizeof(BarInfo));
//
//	
//	if (false == InitDBInfo(ppbarserbroad, cnt))
//	{
//		printf("[DB] InitDBInfo failed\n");
//		return;
//	}
//
//	if (false == CheckDBFile())
//	{
//		printf("[DB] CheckDBFile failed\n");
//		return;
//	}
//
//	if (false == InitBuffer())
//	{
//		printf("[DB] InitBuffer failed\n");
//		return;
//	}
//
//	/*if (false == OpenDB())
//	{
//		printf("[DB] OpenDB failed\n");
//		return;
//	}
//*/
//	
//	
//}


DB::DB(string stringInst, BarSeries_broad** ppbarserbroad, unsigned int cnt)
{
	m_stringInst = stringInst;
	
	if (false == InitDBInfo(ppbarserbroad, cnt))
	{
		printf("[DB] InitDBInfo failed\n");
		return;
	}

	if (false == CheckDBFile())
	{
		printf("[DB] CheckDBFile failed\n");
		return;
	}

	if (false == InitBuffer())
	{
		printf("[DB] InitBuffer failed\n");
		return;
	}
	
}


DB::~DB()
{
	
	//CloseDB();

	DestroyBuffer();

	DestroyDBInfo();

};

bool DB::WriteData()
{
	if (0 ==RUN_STATE || 1 ==RUN_STATE)
	{
	} 
	else
	{
		if (false == OnFireConditon())
		{;
			pantheios::log_NOTICE("[WriteData] ", m_stringInst, ":OnFireConditon Failed");
			return false;
		}
	}
	


	unsigned size = m_vpdbinfo.size();
	unsigned int index = 0;

	for (index = 0; index < size; index++)
	{
		//1. Open DB
		if (false == OpenDB(index))
		{
			printf("[WriteData] OpenDB failed\n");
			return false;
		}


		//2. WriteDB
		if (false == WriteDB(index)) 
		{
			printf("[WriteData] WriteDB failed\n");
			return false;
		}

		//3. Close DB
		if (false == CloseDB(index))
		{
			printf("[WriteData] CloseDB failed\n");
			return false;
		}
	}

	


	return true;
}


bool DB::UpdateDB(unsigned int index)
{

	SQLiteStatement* &curpst = m_vpdbinfo[index]->pst;
	string& dbname = m_vpdbinfo[index]->dbname;
	string& tbName =  m_vpdbinfo[index]->tbname;

	BarSeries_broad* pbarserbroad = m_vpdbinfo[index]->pbarserbroad;
	unsigned int curindex = *(pbarserbroad->m_pindex);
	unsigned int dbindex = pbarserbroad->m_dbindex;

	try
	{
			
		
		memset(m_buffer, 0, sizeof(m_buffer));
		sprintf(m_buffer, "SELECT COUNT(*) FROM %s;", tbName.c_str());
		int cntrecord = (int)curpst->SqlAggregateFuncResult(m_buffer);

		memset(m_buffer, 0, sizeof(m_buffer));
		sprintf(m_buffer, "SELECT MAX(id) FROM %s;", tbName.c_str());
		int maxid = (int)curpst->SqlAggregateFuncResult(m_buffer);


		int insertid = 0;
		if (0 != cntrecord)
		{
			insertid = maxid + 1;
		}



		curpst->BeginTransaction();

		
		BarData* pdbdata = pbarserbroad->m_pcur - (curindex - dbindex);

		SYSTEMTIME* pstart = &pdbdata->starttime;
		SYSTEMTIME* pend = &pdbdata->curtime;
		char starttime[50] = {0};
		char endtime[50] = {0};
		sprintf(starttime, "%d:%d:%d:%d:%d:%d.%d", pstart->wYear, pstart->wMonth, pstart->wDay, pstart->wHour, pstart->wMinute, pstart->wSecond, pstart->wMilliseconds);
		sprintf(endtime, "%d:%d:%d:%d:%d:%d.%d", pend->wYear, pend->wMonth, pend->wDay, pend->wHour, pend->wMinute, pend->wSecond, pend->wMilliseconds);


		memset(m_buffer, 0, sizeof(m_buffer));

		sprintf(m_buffer, "INSERT INTO %s (%s, %s, %s, %s,\
						  %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,\
						  %s, %s, %s, %s, %s,\
						  %s, %s, %s, %s)\
						  VALUES (%d, '%s', '%s', %d,\
						  %0.2f, %0.2f, %0.2f, %0.2f, %d, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f,\
						  '%s', %0.2f, %0.2f, %0.2f, %0.2f,\
						  %0.2f, %d, %0.2f, %d);", 
						  tbName.c_str(), "id", "starttime", "endtime", "tickcnt",
						  "open", "close", "high", "low", "vol", "turnover", "openinterest", "openinterestdiff", "uplimit", "lowlimit",
						  "inst", "exch_open", "exch_close", "exch_high", "exch_low",
						  "bidprice1", "bidvol1", "askprice1", "askvol1",
						  insertid, starttime, endtime, pdbdata->tickcnt, 
						  pdbdata->open, pdbdata->close, pdbdata->high, pdbdata->low, pdbdata->vol, pdbdata->Turnover, pdbdata->OpenInterest, pdbdata->OpenInterestDiff, pdbdata->UpperLimitPrice, pdbdata->LowerLimitPrice,
						  pdbdata->inst, pdbdata->exch_open, pdbdata->exch_close, pdbdata->exch_high, pdbdata->exch_low,
						  pdbdata->BidPrice1, pdbdata->BidVolume1, pdbdata->AskPrice1, pdbdata->AskVolume1);

		//int len = strlen(m_buffer);

		//printf(m_buffer);	


		curpst->SqlStatement(m_buffer);

		curpst->CommitTransaction();
	}
	catch(Kompex::SQLiteException &exception) 
	{
		std::cerr <<"[UpdateDB] "<< dbname.c_str()<<" Exception Occured: " << exception.GetString();

		m_vpdbinfo[index]->pst->RollbackTransaction();
		std::cerr << "[UpdateDB]Rollback was executed!" << std::endl;

		return false;
	}

	return true;
}
//
//bool DB::InsertDB_TEST(unsigned int index, unsigned int base)
//{
//	SQLiteStatement* &curpst = m_vpdbinfo[index]->pst;
//	string& tbName =  m_vpdbinfo[index]->tbname;
//
//	BarSeries_broad* pbarserbroad = m_vpdbinfo[index]->pbarserbroad;
//	unsigned int curindex = *(pbarserbroad->m_pindex);
//	unsigned int dbindex = pbarserbroad->m_dbindex;
//
//
//	try
//	{
//		curpst->BeginTransaction();
//
//		for (unsigned int i = dbindex; i <= curindex; i++ )
//		{
//			BarData* pdbdata = pbarserbroad->m_pcur - (curindex - i);
//
//			if (true == pdbdata->isend)
//			{
//				SYSTEMTIME* pstart = &pdbdata->starttime;
//				SYSTEMTIME* pend = &pdbdata->curtime;
//				char starttime[50] = {0};
//				char endtime[50] = {0};
//				sprintf(starttime, "%d:%d:%d:%d:%d:%d.%d", pstart->wYear, pstart->wMonth, pstart->wDay, pstart->wHour, pstart->wMinute, pstart->wSecond, pstart->wMilliseconds);
//				sprintf(endtime, "%d:%d:%d:%d:%d:%d.%d", pend->wYear, pend->wMonth, pend->wDay, pend->wHour, pend->wMinute, pend->wSecond, pend->wMilliseconds);
//
//
//				memset(m_buffer, 0, sizeof(m_buffer));
//				sprintf(m_buffer, "INSERT INTO %s (%s, %s, %s, %s,\
//								  %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,\
//								  %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,\
//								  %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)\
//								  VALUES (%d, '%s', '%s', %d,\
//								  %0.2f, %0.2f, %0.2f, %0.2f, %d, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f,\
//								  %0.2f, %d, %0.2f, %d, %0.2f, %d, %0.2f, %d, %0.2f, %d,\
//								  %0.2f, %d, %0.2f, %d, %0.2f, %d, %0.2f, %d, %0.2f, %d);", 
//								  tbName, "id", "starttime", "endtime", "tickcnt",
//								  "open", "close", "high", "low", "vol", "turnover", "openinterest", "openinterestdiff", "uplimit", "lowlimit",
//								  "bidprice1", "bidvol1", "bidprice2", "bidvol2", "bidprice3", "bidvol3", "bidprice4", "bidvol4", "bidprice5", "bidvol5",
//								  "askprice1", "askvol1", "askprice2", "askvol2", "askprice3", "askvol3", "askprice4", "askvol4", "askprice5", "askvol5",
//								  base * i, starttime, endtime, pdbdata->tickcnt, 
//								  pdbdata->open, pdbdata->close, pdbdata->high, pdbdata->low, pdbdata->vol, pdbdata->Turnover, pdbdata->OpenInterest, pdbdata->OpenInterestDiff, pdbdata->UpperLimitPrice, pdbdata->LowerLimitPrice,
//								  pdbdata->BidPrice1, pdbdata->BidVolume1, pdbdata->BidPrice2, pdbdata->BidVolume2, pdbdata->BidPrice3, pdbdata->BidVolume3, pdbdata->BidPrice4, pdbdata->BidVolume4, pdbdata->BidPrice5, pdbdata->BidVolume5,
//								  pdbdata->AskPrice1, pdbdata->AskVolume1, pdbdata->AskPrice2, pdbdata->AskVolume2, pdbdata->AskPrice3, pdbdata->AskVolume3, pdbdata->AskPrice4, pdbdata->AskVolume4, pdbdata->AskPrice5, pdbdata->AskVolume5);
//
//				//printf(m_buffer);	
//
//				curpst->SqlStatement(m_buffer);
//
//				//pbarserbroad->m_dbindex = i + 1; 
//			}
//
//		}
//
//		curpst->CommitTransaction();
//
//
//
//	}
//	catch(Kompex::SQLiteException &exception) 
//	{
//		std::cerr << "[InsertDB]Exception Occured: " << exception.GetString();
//
//		m_vpdbinfo[index]->pst->RollbackTransaction();
//		std::cerr << "[InsertDB]Rollback was executed!" << std::endl;
//
//		return false;
//	}
//
//
//	return true;
//
//
//}



bool DB::InsertDB(unsigned int index)
{
	SQLiteStatement* &curpst = m_vpdbinfo[index]->pst;
	string& dbname = m_vpdbinfo[index]->dbname;
	string& tbName =  m_vpdbinfo[index]->tbname;

	BarSeries_broad* pbarserbroad = m_vpdbinfo[index]->pbarserbroad;
	unsigned int curindex = *(pbarserbroad->m_pindex);
	unsigned int dbindex = pbarserbroad->m_dbindex;
	
	
	try
	{
		curpst->BeginTransaction();

		for (unsigned int i = dbindex; i <= curindex; i++ )
		{
			BarData* pdbdata = pbarserbroad->m_pcur - (curindex - i);

			if (true == pdbdata->isend)
			{
				SYSTEMTIME* pstart = &pdbdata->starttime;
				SYSTEMTIME* pend = &pdbdata->curtime;
				char starttime[50] = {0};
				char endtime[50] = {0};
				sprintf(starttime, "%d:%d:%d:%d:%d:%d.%d", pstart->wYear, pstart->wMonth, pstart->wDay, pstart->wHour, pstart->wMinute, pstart->wSecond, pstart->wMilliseconds);
				sprintf(endtime, "%d:%d:%d:%d:%d:%d.%d", pend->wYear, pend->wMonth, pend->wDay, pend->wHour, pend->wMinute, pend->wSecond, pend->wMilliseconds);


				memset(m_buffer, 0, sizeof(m_buffer));
				sprintf(m_buffer, "INSERT INTO %s (%s, %s, %s, %s,\
								  %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,\
								  %s, %s, %s, %s, %s,\
								  %s, %s, %s, %s)\
								  VALUES (%d, '%s', '%s', %d,\
								  %0.2f, %0.2f, %0.2f, %0.2f, %d, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f,\
								  '%s', %0.2f, %0.2f, %0.2f, %0.2f,\
								  %0.2f, %d, %0.2f, %d);", 
								  tbName.c_str(), "id", "starttime", "endtime", "tickcnt",
								  "open", "close", "high", "low", "vol", "turnover", "openinterest", "openinterestdiff", "uplimit", "lowlimit",
								  "inst", "exch_open", "exch_close", "exch_high", "exch_low",
								  "bidprice1", "bidvol1", "askprice1", "askvol1",
								  i, starttime, endtime, pdbdata->tickcnt, 
								  pdbdata->open, pdbdata->close, pdbdata->high, pdbdata->low, pdbdata->vol, pdbdata->Turnover, pdbdata->OpenInterest, pdbdata->OpenInterestDiff, pdbdata->UpperLimitPrice, pdbdata->LowerLimitPrice,
								  pdbdata->inst, pdbdata->exch_open, pdbdata->exch_close, pdbdata->exch_high, pdbdata->exch_low,
								  pdbdata->BidPrice1, pdbdata->BidVolume1, pdbdata->AskPrice1, pdbdata->AskVolume1);

				//int len = strlen(m_buffer);

				//printf(m_buffer);	 

				curpst->SqlStatement(m_buffer);

				//pbarserbroad->m_dbindex = i + 1; 
			}
		
		}

		curpst->CommitTransaction();

		

	}
	catch(Kompex::SQLiteException &exception) 
	{
		std::cerr <<"[InsertDB] "<< dbname.c_str()<<" Exception Occured: " << exception.GetString();
		m_vpdbinfo[index]->pst->RollbackTransaction();
		std::cerr << "[InsertDB] Rollback was executed!" << std::endl;


		return false;
	}


	return true;


}
//
//
//bool DB::UpdateCSV(unsigned int index)
//{
//	BarSeries_broad* pbarserbroad = m_vpdbinfo[index]->pbarserbroad;
//	unsigned int curindex = *(pbarserbroad->m_pindex);
//	unsigned int dbindex = pbarserbroad->m_dbindex;
//
//
//	string& pcsv = m_vpdbinfo[index]->csvname;
//	boost::filesystem::ofstream out(pcsv, ios::out);
//
//	if (out)
//	{
//
//		BarData* pdbdata = pbarserbroad->m_pcur - (curindex - dbindex);
//	
//		SYSTEMTIME* pstart = &pdbdata->starttime;
//		SYSTEMTIME* pend = &pdbdata->curtime;
//		char starttime[50] = {0};
//		char endtime[50] = {0};
//		sprintf(starttime, "%d:%d:%d:%d:%d:%d.%d", pstart->wYear, pstart->wMonth, pstart->wDay, pstart->wHour, pstart->wMinute, pstart->wSecond, pstart->wMilliseconds);
//		sprintf(endtime, "%d:%d:%d:%d:%d:%d.%d", pend->wYear, pend->wMonth, pend->wDay, pend->wHour, pend->wMinute, pend->wSecond, pend->wMilliseconds);
//
//
//		out << boost::format("%1%,%2%,%3%,%4%,%5%,%6%,%7%,%8%,%9%,%10%,%11%,%12%,%13%,%14%,%15%,%16%,%17%,%18%,%19%,%20%,%21%,%22%,%23%,%24%,%25%,%26%,%27%,%28%,%29%,%30%,%31%,%32%,%33%,%34%")
//			% dbindex % starttime % endtime % pdbdata->tickcnt
//			% pdbdata->open % pdbdata->close % pdbdata->high % pdbdata->low % pdbdata->vol % pdbdata->Turnover % pdbdata->OpenInterest % pdbdata->OpenInterestDiff % pdbdata->UpperLimitPrice % pdbdata->LowerLimitPrice
//			% pdbdata->BidPrice1 % pdbdata->BidVolume1 % pdbdata->BidPrice2 % pdbdata->BidVolume2 % pdbdata->BidPrice3 % pdbdata->BidVolume3 % pdbdata->BidPrice4 % pdbdata->BidVolume4 % pdbdata->BidPrice5 % pdbdata->BidVolume5
//			% pdbdata->AskPrice1 % pdbdata->AskVolume1 % pdbdata->AskPrice2 % pdbdata->AskVolume2 % pdbdata->AskPrice3 % pdbdata->AskVolume3 % pdbdata->AskPrice4 % pdbdata->AskVolume4 % pdbdata->AskPrice5 % pdbdata->AskVolume5
//			<< endl;
//
//
//	
//		out.close();
//
//	}
//	else
//	{
//		SetConsoleColor(FOREGROUND_GREEN);  
//		printf("[UpdateCSV()] openfilefailed\n");
//		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
//
//		return false;
//	}
//
//
//	return true;
//}
//
//bool DB::InsertCSV(unsigned int index)
//{
//
//	BarSeries_broad* pbarserbroad = m_vpdbinfo[index]->pbarserbroad;
//	unsigned int curindex = *(pbarserbroad->m_pindex);
//	unsigned int dbindex = pbarserbroad->m_dbindex;
//
//
//	string& pcsv = m_vpdbinfo[index]->csvname;
//	boost::filesystem::ofstream out(pcsv, ios::app);
//
//	if (out)
//	{
//
//		for (unsigned int i = dbindex; i <= curindex; i++ )
//		{
//
//			BarData* pdbdata = pbarserbroad->m_pcur - (curindex - i);
//
//			if (true == pdbdata->isend)
//			{
//				SYSTEMTIME* pstart = &pdbdata->starttime;
//				SYSTEMTIME* pend = &pdbdata->curtime;
//				char starttime[50] = {0};
//				char endtime[50] = {0};
//				sprintf(starttime, "%d:%d:%d:%d:%d:%d.%d", pstart->wYear, pstart->wMonth, pstart->wDay, pstart->wHour, pstart->wMinute, pstart->wSecond, pstart->wMilliseconds);
//				sprintf(endtime, "%d:%d:%d:%d:%d:%d.%d", pend->wYear, pend->wMonth, pend->wDay, pend->wHour, pend->wMinute, pend->wSecond, pend->wMilliseconds);
//
//
//				out << boost::format("%1%,%2%,%3%,%4%,%5%,%6%,%7%,%8%,%9%,%10%,%11%,%12%,%13%,%14%,%15%,%16%,%17%,%18%,%19%,%20%,%21%,%22%,%23%,%24%,%25%,%26%,%27%,%28%,%29%,%30%,%31%,%32%,%33%,%34%")
//					% i % starttime % endtime % pdbdata->tickcnt
//					% pdbdata->open % pdbdata->close % pdbdata->high % pdbdata->low % pdbdata->vol % pdbdata->Turnover % pdbdata->OpenInterest % pdbdata->OpenInterestDiff % pdbdata->UpperLimitPrice % pdbdata->LowerLimitPrice
//					% pdbdata->BidPrice1 % pdbdata->BidVolume1 % pdbdata->BidPrice2 % pdbdata->BidVolume2 % pdbdata->BidPrice3 % pdbdata->BidVolume3 % pdbdata->BidPrice4 % pdbdata->BidVolume4 % pdbdata->BidPrice5 % pdbdata->BidVolume5
//					% pdbdata->AskPrice1 % pdbdata->AskVolume1 % pdbdata->AskPrice2 % pdbdata->AskVolume2 % pdbdata->AskPrice3 % pdbdata->AskVolume3 % pdbdata->AskPrice4 % pdbdata->AskVolume4 % pdbdata->AskPrice5 % pdbdata->AskVolume5
//					<< endl;
//
//				pbarserbroad->m_dbindex = i + 1; 
//			}
//
//			
//		}
//
//
//		out.close();
//
//	}
//	else
//	{
//		SetConsoleColor(FOREGROUND_GREEN);  
//		printf("[InsertCSV()] openfilefailed\n");
//		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
//
//		return false;
//	}
//
//
//	return true;
//}

bool DB::UpdateMemDB(unsigned int index)
{
	BarSeries_broad* pbarserbroad = m_vpdbinfo[index]->pbarserbroad;
	unsigned int curindex = *(pbarserbroad->m_pindex);
	unsigned int dbindex = pbarserbroad->m_dbindex;

	bool bupdated = false;
	int print_start = -1;
	int print_end = -1;

	//update print_start and print_end and bupdated 
	for (unsigned int i = dbindex; i <= curindex; i++ )
	{
		BarData* pdbdata = pbarserbroad->m_pcur - (curindex - i);

		if (true == pdbdata->isend)
		{
			if (-1 == print_start)
			{
				print_start = i;
				print_end = i;
			}			
			else
				print_end = i;

			bupdated = true;
		}
	}

	//print above variable
	if (bupdated)
	{
		pbarserbroad->m_dbindex = print_end + 1;

		char type[10] = {0};
		if (0 == index)
			sprintf(type, "%s", "tick"); 
		else if (1 == index)
			sprintf(type, "%s", "sec"); 
		else if (2 == index)
			sprintf(type, "%s", "min"); 
		else if (3 == index)
			sprintf(type, "%s", "day"); 


		char* inst = pbarserbroad->m_serinfo.barinfo.instru;
		//printf("[UpdateMemDB] (%s:%s) Record(%d~%d)\n", inst, type, print_start, print_end);
		pantheios::log_NOTICE("[UpdateMemDB] (", inst, ":", type, ") Record(", panint(print_start), "~", panint(print_end), ")");
	}

	
	return true;
}



bool DB::OpenDB(unsigned int index)
{

	//unsigned size = m_vpdbinfo.size();
	//for (unsigned int index = 0; index < size; index++)
	//{
	try
	{
		SQLiteDatabase* &curpdb = m_vpdbinfo[index]->pdb;
		SQLiteStatement* &curpst = m_vpdbinfo[index]->pst;
		string& curdbname = m_vpdbinfo[index]->dbname;
		string& curtbname = m_vpdbinfo[index]->tbname;

		/**********************Open DB********************************************/

		curpdb = new Kompex::SQLiteDatabase(curdbname, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
		// create statement instance for sql queries/statements
		curpst = new Kompex::SQLiteStatement(curpdb);

		//cout << "SQLite version: " << curpdb->GetLibVersionNumber() << std::endl;

		/************************************************************************/


		/**********************Create Table*****************************************/
		memset(m_buffer, 0, sizeof(m_buffer));
		sprintf(m_buffer, "select name from sqlite_master where type='table' and name = '%s';", curtbname.c_str());

		const char *tbName = (const char*)curpst->GetSqlResultCString(m_buffer);

		//curpst->FreeQuery();

		if (NULL == tbName)//table note exist, create it
		{
			memset(m_buffer, 0, sizeof(m_buffer));
		/*	sprintf(m_buffer, "CREATE TABLE %s (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);", 
			curtbname.c_str(), "id INTEGER NOT NULL PRIMARY KEY", "tickcnt INTEGER", "starttime VARCHAR(20)", "endtime VARCHAR(20)",
			"open DOUBLE", "close DOUBLE", "high DOUBLE", "low DOUBLE", "vol INTEGER", "turnover DOUBLE", "openinterest DOUBLE", "openinterestdiff DOUBLE", "uplimit DOUBLE", "lowlimit DOUBLE",
			"inst VARCHAR(15)", "exch_open DOUBLE", "exch_close DOUBLE", "exch_high DOUBLE", "exch_low DOUBLE",
			"bidprice1 DOUBLE", "bidvol1 INTEGER", "bidprice2 DOUBLE", "bidvol2 INTEGER", "bidprice3 DOUBLE", "bidvol3 INTEGER", "bidprice4 DOUBLE", "bidvol4 INTEGER", "bidprice5 DOUBLE", "bidvol5 INTEGER",
			"askprice1 DOUBLE", "askvol1 INTEGER", "askprice2 DOUBLE", "askvol2 INTEGER", "askprice3 DOUBLE", "askvol3 INTEGER", "askprice4 DOUBLE", "askvol4 INTEGER", "askprice5 DOUBLE", "askvol5 INTEGER");*/
			

			sprintf(m_buffer, "CREATE TABLE %s (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);", 
			curtbname.c_str(), "id INTEGER NOT NULL PRIMARY KEY", "tickcnt INTEGER", "starttime VARCHAR(20)", "endtime VARCHAR(20)",
			"open DOUBLE", "close DOUBLE", "high DOUBLE", "low DOUBLE", "vol INTEGER", "turnover DOUBLE", "openinterest DOUBLE", "openinterestdiff DOUBLE", "uplimit DOUBLE", "lowlimit DOUBLE",
			"inst VARCHAR(15)", "exch_open DOUBLE", "exch_close DOUBLE", "exch_high DOUBLE", "exch_low DOUBLE",
			"bidprice1 DOUBLE", "bidvol1 INTEGER", "askprice1 DOUBLE", "askvol1 INTEGER");


			//printf(m_buffer);


			//int len = strlen(m_buffer);

			curpst->SqlStatement(m_buffer);


			//curpst->SqlStatement("PRAGMA synchronous=OFF;");
		}
		else//exist talbe for second write
		{
			//SetConsoleColor(FOREGROUND_GREEN);  
			//printf("[OpenDB] exist table:%s\n", curtbname.c_str());
			//SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); 

			pantheios::log_NOTICE("[OpenDB] exist table:", curtbname.c_str());


		}

		

		/************************************************************************/

	}
	catch(Kompex::SQLiteException &exception)
	{
		std::cerr << "[OpenDB] Exception Occured" << std::endl;
		exception.Show();
		return false;
	}
	//}

	return true;
}

bool DB::CloseDB(unsigned int index)
{
	//unsigned size = m_vpdbinfo.size();
	//for (unsigned int index = 0; index < size; index++)
	//{
	try
	{
		SQLiteDatabase* &curpdb = m_vpdbinfo[index]->pdb;
		SQLiteStatement* &curpst = m_vpdbinfo[index]->pst;
		string& curdbname = m_vpdbinfo[index]->dbname;
		string& curtbname = m_vpdbinfo[index]->tbname;

		/**********************Close DB********************************************/

		if (curpdb)
		{
			delete curpdb;
		}

		if (curpst)
		{
			delete curpst;
		}

		/************************************************************************/


	}
	catch(Kompex::SQLiteException &exception)
	{
		std::cerr << "[CloseDB] \nException Occured" << std::endl;
		exception.Show();
		return false;
	}

	//}

	return true;
}

bool DB::WriteDB(unsigned int index)
{
	unsigned size = m_vpdbinfo.size();

	if (index < size -1)
	{
		/*for (int i = 1; i <= 1000; i++)
			InsertDB_TEST(index, i);*/


		if (false == InsertDB(index))
		{
			printf("[WriteDB] InsertDB failed\n");
			return false;
		}



		UpdateMemDB(index);

	}
	else
	{
		if (false == UpdateDB(index))
		{
			printf("[WriteDB] UpdateDB failed\n");
			return false;
		}

	}

	return true;
}


bool DB::InitDBInfo(BarSeries_broad** ppbarserbroad, unsigned int cnt)
{
	for (unsigned int i = 0; i < cnt; i++)
	{
		string bartype;
		string dbname;
		string tbname;
		string csvname;		
		string dbfolder;
		string csvfolder;

		BarSeries_broad* pbarserbroad = ppbarserbroad[i];

		char* inst = pbarserbroad->m_serinfo.barinfo.instru;

		if (Bartype::tick == pbarserbroad->m_serinfo.barinfo.type)
			bartype = "tick";	
		else if (Bartype::sec == pbarserbroad->m_serinfo.barinfo.type)
			bartype = "sec"; 		
		else if (Bartype::min == pbarserbroad->m_serinfo.barinfo.type)
			bartype = "min";	
		else if (Bartype::day == pbarserbroad->m_serinfo.barinfo.type)
			bartype = "day";


		string prefix = FOLDER_PREFIX;
		if (0 ==RUN_STATE || 1 ==RUN_STATE)
		{
			dbfolder = prefix + "/" + inst + "/" + bartype + "/db/test";
			csvfolder = prefix + "/" + inst + "/" + bartype + "/csv/test";
		} 
		else
		{
			dbfolder = prefix + "/" + inst + "/" + bartype + "/db";
			csvfolder = prefix + "/" + inst + "/" + bartype + "/csv";
		}


		dbname = dbfolder + "/" + inst + "_" + gTradingDay + "_" + bartype;
		csvname = csvfolder + "/" + inst + "_" + gTradingDay + "_" + bartype;
		tbname = bartype + "table";


		DBINFO* pdbinfo = new DBINFO(dbname, tbname, csvname, dbfolder, csvfolder, pbarserbroad);
		if (NULL == pdbinfo)
		{
			printf("[InitDBInfo] pdbinfo init failed\n");
			return false;
		}

		m_vpdbinfo.push_back(pdbinfo);

	}



	return true;
}

void DB::DestroyDBInfo()
{
	unsigned size = m_vpdbinfo.size();
	for (unsigned int index = 0; index < size; index++)
	{
		DBINFO* pdbinfo = m_vpdbinfo[index];

		if (pdbinfo)
		{
			delete pdbinfo;
		}


	}

}







bool DB::InitBuffer()
{
	m_buffer = new char[1024];//1K
	if (NULL == m_buffer)
	{
		printf("[InitBuffer] m_buffer NULL\n");
		return false;
	}
	return true;

}

void DB::DestroyBuffer()
{
	if (m_buffer)
	{
		delete[] m_buffer;
	}
}



bool DB::CheckDBFile()
{
	
	unsigned size = m_vpdbinfo.size();
	for (unsigned int index = 0; index < size; index++)
	{
		/*********************Cretate Parent and sub folder********************************/
		string& dbfolder = m_vpdbinfo[index]->dbfolder;
		string& csvfolder = m_vpdbinfo[index]->csvfolder;

		try
		{
			path dbf(dbfolder);
			path csvf(csvfolder);

			//check parent folder exist?
			if (!exists(dbf) || !is_directory(dbf))
			{
				
				if (create_directories(dbf))
					printf("[CheckDBFile] Create dbf:%s successed\n", dbf.string().c_str());
				else
				{
					printf("[CheckDBFile] Create dbf:%s failed\n", dbf.string().c_str());
					return false;
				}	
			}

			if (!exists(csvf) || !is_directory(csvf))
			{
				if (create_directories(csvf))
					printf("[CheckDBFile] Create csvf:%s successed\n", csvf.string().c_str());
				else
				{
					printf("[CheckDBFile] Create csvf:%s failed\n", csvf.string().c_str());
					return false;
				}	
			}


		}
		catch (const filesystem_error& ex)
		{
			cout << ex.what() << '\n';
			return false;
		}


		/***************************************************************************************************/
		

		/********************check dbfile exist? yes then delete it.****Init m_vdbname**********************/

		string dbfile = m_vpdbinfo[index]->dbname;
		string csvfile = m_vpdbinfo[index]->csvname;

		try
		{
			path dbf(dbfile);

			if (exists(dbf) && !is_directory(dbf))
			{
				if (remove(dbf) == false)
				{
					printf("[CheckDBFile] remove:%s failed\n", dbf.filename().string());
					return false;
				}
			}

			path csvf(csvfile);

			if (exists(csvf) && !is_directory(csvf))
			{
				if (remove(csvf) == false)
				{
					printf("[CheckDBFile] remove:%s failed\n", csvf.filename().string());
					return false;
				}
			}

		}
		catch (const filesystem_error& ex)
		{
			cout << ex.what() << '\n';
			return false;
		}

		/***************************************************************************************************/
		
	}


	return true;
}


bool DB::OnFireConditon()
{
	unsigned size = m_vpdbinfo.size();
	for (unsigned int t = 0; t < size - 1; t++)
	{
		if (false == SetDBLastbar(t))
		{
			pantheios::log_ERROR("[SatisfyFireConditon] SetDBLastbar(", panint(t),") false");

			//return false;
		}
	}

	return true;


	//SYSTEMTIME curtime, db_firetime_1_start, db_firetime_1_end;
	//GetLocalTime(&curtime);
	//GetLocalTime(&db_firetime_1_start);
	//GetLocalTime(&db_firetime_1_end);


	//db_firetime_1_start.wHour = DB_FIRETIME_1_STARTHOUR;
	//db_firetime_1_start.wMinute = DB_FIRETIME_1_STARTMIN;
	//db_firetime_1_start.wSecond = DB_FIRETIME_1_STARTSEC;
	//db_firetime_1_end.wHour = DB_FIRETIME_1_ENDHOUR;
	//db_firetime_1_end.wMinute = DB_FIRETIME_1_ENDMIN;
	//db_firetime_1_end.wSecond = DB_FIRETIME_1_ENDSEC;


	//CTime cur_ctime(curtime);
	//CTime start_ctime(db_firetime_1_start);
	//CTime end_ctime(db_firetime_1_end);
	//CTimeSpan startspan = cur_ctime - start_ctime;
	//CTimeSpan endspan = end_ctime - cur_ctime;
	//


	//if (startspan.GetTotalSeconds() >= 0 && endspan.GetTotalSeconds() >= 0)
	//{
	//	unsigned size = m_vpdbinfo.size();
	//	for (unsigned int t = 0; t < size; t++)
	//	{
	//		if (false == SetDBLastbar(t))
	//		{
	//			SetConsoleColor(FOREGROUND_GREEN);  
	//			printf("[SatisfyFireConditon] SetDBLastbar(%d) false\n", t);
	//			SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
	//			//return false;
	//		}
	//	}
	//}

	//return true;
}



bool DB::SetDBLastbar(unsigned int index)
{

	BarSeries_broad* pbarserbroad = m_vpdbinfo[index]->pbarserbroad;

	if (true == pbarserbroad->GetBarend())
	{
		printf("[SetDBLastbar] already true\n");
		return false;
	}
	
	pbarserbroad->SetBarend();

	return true;
}



//
//
//bool DB::UpdateDB(unsigned int index)
//{
//	SQLiteStatement* &curpst = m_vpdbinfo[index]->pst;
//	char* tbName =  m_vpdbinfo[index]->tbname;
//
//	BarSeries_broad* pbarserbroad = m_vpdbinfo[index]->pbarserbroad;
//	unsigned int curindex = *(pbarserbroad->m_pindex);
//	unsigned int dbindex = pbarserbroad->m_dbindex;
//
//
//
//	try
//	{
//		bool binsert = false;
//
//		memset(m_buffer, 0, sizeof(m_buffer));
//		sprintf(m_buffer, "select id from %s where id = %d;", tbName, dbindex);
//
//		int selid = -1;
//		selid = curpst->GetSqlResultInt(m_buffer);
//
//		if (-1 == selid)//not serach it so insert
//		{
//			binsert = true;
//		} 
//
//
//		curpst->BeginTransaction();
//
//		if (false == binsert)
//		{
//			BarData* pdbdata = pbarserbroad->m_pcur - (curindex - dbindex);
//
//			SYSTEMTIME* pend = &pdbdata->curtime;
//			char endtime[50] = {0};
//			sprintf(endtime, "%d:%d:%d:%d:%d:%d.%d", pend->wYear, pend->wMonth, pend->wDay, pend->wHour, pend->wMinute, pend->wSecond, pend->wMilliseconds);
//
//			memset(m_buffer, 0, sizeof(m_buffer));
//			sprintf(m_buffer, "UPDATE %s SET endtime='%s', tickcnt=%d,\
//							  open=%0.2f, close=%0.2f, high=%0.2f, low=%0.2f, vol=%d, turnover=%0.2f, openinterest=%0.2f, openinterestdiff=%0.2f,\
//							  bidprice1=%0.2f, bidvol1=%d, bidprice2=%0.2f, bidvol2=%d, bidprice3=%0.2f, bidvol3=%d, bidprice4=%0.2f, bidvol4=%d, bidprice5=%0.2f, bidvol5=%d,\
//							  askprice1=%0.2f, askvol1=%d, askprice2=%0.2f, askvol2=%d, askprice3=%0.2f, askvol3=%d, askprice4=%0.2f, askvol4=%d, askprice5=%0.2f, askvol5=%d\
//							  where id=%d;",
//							  tbName, endtime, pdbdata->tickcnt, 
//							  pdbdata->open, pdbdata->close, pdbdata->high, pdbdata->low, pdbdata->vol, pdbdata->Turnover, pdbdata->OpenInterest, pdbdata->OpenInterestDiff,
//							  pdbdata->BidPrice1, pdbdata->BidVolume1, pdbdata->BidPrice2, pdbdata->BidVolume2, pdbdata->BidPrice3, pdbdata->BidVolume3, pdbdata->BidPrice4, pdbdata->BidVolume4, pdbdata->BidPrice5, pdbdata->BidVolume5,
//							  pdbdata->AskPrice1, pdbdata->AskVolume1, pdbdata->AskPrice2, pdbdata->AskVolume2, pdbdata->AskPrice3, pdbdata->AskVolume3, pdbdata->AskPrice4, pdbdata->AskVolume4, pdbdata->AskPrice5, pdbdata->AskVolume5,
//							  dbindex);
//		} 
//		else
//		{
//
//			BarData* pdbdata = pbarserbroad->m_pcur - (curindex - dbindex);
//
//			SYSTEMTIME* pstart = &pdbdata->starttime;
//			SYSTEMTIME* pend = &pdbdata->curtime;
//			char starttime[50] = {0};
//			char endtime[50] = {0};
//			sprintf(starttime, "%d:%d:%d:%d:%d:%d.%d", pstart->wYear, pstart->wMonth, pstart->wDay, pstart->wHour, pstart->wMinute, pstart->wSecond, pstart->wMilliseconds);
//			sprintf(endtime, "%d:%d:%d:%d:%d:%d.%d", pend->wYear, pend->wMonth, pend->wDay, pend->wHour, pend->wMinute, pend->wSecond, pend->wMilliseconds);
//
//
//			memset(m_buffer, 0, sizeof(m_buffer));
//			sprintf(m_buffer, "INSERT INTO %s (%s, %s, %s, %s,\
//							  %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,\
//							  %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,\
//							  %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)\
//							  VALUES (%d, '%s', '%s', %d,\
//							  %0.2f, %0.2f, %0.2f, %0.2f, %d, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f,\
//							  %0.2f, %d, %0.2f, %d, %0.2f, %d, %0.2f, %d, %0.2f, %d,\
//							  %0.2f, %d, %0.2f, %d, %0.2f, %d, %0.2f, %d, %0.2f, %d);", 
//							  tbName, "id", "starttime", "endtime", "tickcnt",
//							  "open", "close", "high", "low", "vol", "turnover", "openinterest", "openinterestdiff", "uplimit", "lowlimit",
//							  "bidprice1", "bidvol1", "bidprice2", "bidvol2", "bidprice3", "bidvol3", "bidprice4", "bidvol4", "bidprice5", "bidvol5",
//							  "askprice1", "askvol1", "askprice2", "askvol2", "askprice3", "askvol3", "askprice4", "askvol4", "askprice5", "askvol5",
//							  dbindex, starttime, endtime, pdbdata->tickcnt, 
//							  pdbdata->open, pdbdata->close, pdbdata->high, pdbdata->low, pdbdata->vol, pdbdata->Turnover, pdbdata->OpenInterest, pdbdata->OpenInterestDiff, pdbdata->UpperLimitPrice, pdbdata->LowerLimitPrice,
//							  pdbdata->BidPrice1, pdbdata->BidVolume1, pdbdata->BidPrice2, pdbdata->BidVolume2, pdbdata->BidPrice3, pdbdata->BidVolume3, pdbdata->BidPrice4, pdbdata->BidVolume4, pdbdata->BidPrice5, pdbdata->BidVolume5,
//							  pdbdata->AskPrice1, pdbdata->AskVolume1, pdbdata->AskPrice2, pdbdata->AskVolume2, pdbdata->AskPrice3, pdbdata->AskVolume3, pdbdata->AskPrice4, pdbdata->AskVolume4, pdbdata->AskPrice5, pdbdata->AskVolume5);
//
//			//printf(m_buffer);	
//		}
//
//
//		curpst->SqlStatement(m_buffer);
//
//		curpst->CommitTransaction();
//	}
//	catch(Kompex::SQLiteException &exception) 
//	{
//		std::cerr << "Exception Occured: " << exception.GetString();
//
//		m_vpdbinfo[index]->pst->RollbackTransaction();
//		std::cerr << "Rollback was executed!" << std::endl;
//		std::cerr << "This is our own catch() block!" << std::endl;
//
//		return false;
//	}
//
//
//	return true;
//
//	////printf(m_buffer);	
//	//char bartype[5] = {0};
//	//if (m_pbar->m_serinfo.barinfo.type == tick)
//	//	sprintf(bartype, "%s", "tick");
//	//else if (m_pbar->m_serinfo.barinfo.type == sec)
//	//	sprintf(bartype, "%s", "sec");
//	//else if (m_pbar->m_serinfo.barinfo.type == Bartype::min)
//	//	sprintf(bartype, "%s", "min");
//	//else if (m_pbar->m_serinfo.barinfo.type == day)
//	//	sprintf(bartype, "%s", "day");
//
//
//	//printf("[WriteData] UPDATE %s len:%d\n", bartype, strlen(m_buffer));
//}
//
//

//
//bool DB::WriteData()
//{
//	unsigned int tickcnt = m_pbar->m_pcur->tickcnt;
//
//	if (1 != tickcnt)
//	{
//		//pStmt->SqlStatement("UPDATE user SET weight=51.5, age=18 WHERE firstName='Carlene'");
//		/*********************Update db exist record********************************/
//		SYSTEMTIME* pend = &m_pbar->m_pcur->curtime;
//		char endtime[50] = {0};
//		sprintf(endtime, "%d:%d:%d:%d:%d:%d.%d", pend->wYear, pend->wMonth, pend->wDay, pend->wHour, pend->wMinute, pend->wSecond, pend->wMilliseconds);
//
//		BarData* pbardata = m_pbar->m_pcur;
//		memset(m_buffer, 0, sizeof(m_buffer));
//		sprintf(m_buffer, "UPDATE %s SET endtime='%s', tickcnt=%d,\
//						open=%0.2f, close=%0.2f, high=%0.2f, low=%0.2f, vol=%d, turnover=%0.2f, openinterest=%0.2f, openinterestdiff=%0.2f,\
//						bidprice1=%0.2f, bidvol1=%d, bidprice2=%0.2f, bidvol2=%d, bidprice3=%0.2f, bidvol3=%d, bidprice4=%0.2f, bidvol4=%d, bidprice5=%0.2f, bidvol5=%d,\
//						askprice1=%0.2f, askvol1=%d, askprice2=%0.2f, askvol2=%d, askprice3=%0.2f, askvol3=%d, askprice4=%0.2f, askvol4=%d, askprice5=%0.2f, askvol5=%d\
//						where id=%d;",
//					   m_tbname, endtime, pbardata->tickcnt, 
//					   pbardata->open, pbardata->close, pbardata->high, pbardata->low, pbardata->vol, pbardata->Turnover, pbardata->OpenInterest, pbardata->OpenInterestDiff,
//					   pbardata->BidPrice1, pbardata->BidVolume1, pbardata->BidPrice2, pbardata->BidVolume2, pbardata->BidPrice3, pbardata->BidVolume3, pbardata->BidPrice4, pbardata->BidVolume4, pbardata->BidPrice5, pbardata->BidVolume5,
//					   pbardata->AskPrice1, pbardata->AskVolume1, pbardata->AskPrice2, pbardata->AskVolume2, pbardata->AskPrice3, pbardata->AskVolume3, pbardata->AskPrice4, pbardata->AskVolume4, pbardata->AskPrice5, pbardata->AskVolume5,
//					   m_pbar->m_index);
//
//
//		////printf(m_buffer);	
//		//char bartype[5] = {0};
//		//if (m_pbar->m_serinfo.barinfo.type == tick)
//		//	sprintf(bartype, "%s", "tick");
//		//else if (m_pbar->m_serinfo.barinfo.type == sec)
//		//	sprintf(bartype, "%s", "sec");
//		//else if (m_pbar->m_serinfo.barinfo.type == Bartype::min)
//		//	sprintf(bartype, "%s", "min");
//		//else if (m_pbar->m_serinfo.barinfo.type == day)
//		//	sprintf(bartype, "%s", "day");
//
//
//		//printf("[WriteData] UPDATE %s len:%d\n", bartype, strlen(m_buffer));
//
//
//	
//		
//
//		/************************************************************************/
//	}
//	else if (1 == tickcnt)
//	{
//		/*********************insert db new record********************************/
//		SYSTEMTIME* pstart = &m_pbar->m_pcur->starttime;
//		SYSTEMTIME* pend = &m_pbar->m_pcur->curtime;
//		char starttime[50] = {0};
//		char endtime[50] = {0};
//		sprintf(starttime, "%d:%d:%d:%d:%d:%d.%d", pstart->wYear, pstart->wMonth, pstart->wDay, pstart->wHour, pstart->wMinute, pstart->wSecond, pstart->wMilliseconds);
//		sprintf(endtime, "%d:%d:%d:%d:%d:%d.%d", pend->wYear, pend->wMonth, pend->wDay, pend->wHour, pend->wMinute, pend->wSecond, pend->wMilliseconds);
//
//		BarData* pbardata = m_pbar->m_pcur;
//		memset(m_buffer, 0, sizeof(m_buffer));
//		sprintf(m_buffer, "INSERT INTO %s (%s, %s, %s, %s,\
//					   %s, %s, %s, %s, %s, %s, %s, %s,\
//					   %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,\
//					   %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)\
//					   VALUES (%d, '%s', '%s', %d,\
//					   %0.2f, %0.2f, %0.2f, %0.2f, %d, %0.2f, %0.2f, %0.2f,\
//					   %0.2f, %d, %0.2f, %d, %0.2f, %d, %0.2f, %d, %0.2f, %d,\
//					   %0.2f, %d, %0.2f, %d, %0.2f, %d, %0.2f, %d, %0.2f, %d);", 
//					   m_tbname, "id", "starttime", "endtime", "tickcnt",
//					   "open", "close", "high", "low", "vol", "turnover", "openinterest", "openinterestdiff",
//					   "bidprice1", "bidvol1", "bidprice2", "bidvol2", "bidprice3", "bidvol3", "bidprice4", "bidvol4", "bidprice5", "bidvol5",
//					   "askprice1", "askvol1", "askprice2", "askvol2", "askprice3", "askvol3", "askprice4", "askvol4", "askprice5", "askvol5",
//					   m_pbar->m_index, starttime, endtime, pbardata->tickcnt, 
//					   pbardata->open, pbardata->close, pbardata->high, pbardata->low, pbardata->vol, pbardata->Turnover, pbardata->OpenInterest, pbardata->OpenInterestDiff,
//					   pbardata->BidPrice1, pbardata->BidVolume1, pbardata->BidPrice2, pbardata->BidVolume2, pbardata->BidPrice3, pbardata->BidVolume3, pbardata->BidPrice4, pbardata->BidVolume4, pbardata->BidPrice5, pbardata->BidVolume5,
//					   pbardata->AskPrice1, pbardata->AskVolume1, pbardata->AskPrice2, pbardata->AskVolume2, pbardata->AskPrice3, pbardata->AskVolume3, pbardata->AskPrice4, pbardata->AskVolume4, pbardata->AskPrice5, pbardata->AskVolume5);
//
//
//		//printf(m_buffer);	
//		//printf("[WriteData] size is:%d\n", strlen(m_buffer));
//		/*char bartype[5] = {0};
//		if (m_pbar->m_serinfo.barinfo.type == tick)
//			sprintf(bartype, "%s", "tick");
//		else if (m_pbar->m_serinfo.barinfo.type == sec)
//			sprintf(bartype, "%s", "sec");
//		else if (m_pbar->m_serinfo.barinfo.type == Bartype::min)
//			sprintf(bartype, "%s", "min");
//		else if (m_pbar->m_serinfo.barinfo.type == day)
//			sprintf(bartype, "%s", "day");
//
//
//		printf("[WriteData] INSERT %s len:%d\n", bartype, strlen(m_buffer));*/
//
//		//m_pst->SqlStatement(m_buffer);
//
//		/************************************************************************/
//	}
//
//
//
//	try
//	{
//
//		m_pst->BeginTransaction();
//
//		m_pst->SqlStatement(m_buffer);
//
//		m_pst->CommitTransaction();
//	}
//	catch(Kompex::SQLiteException &exception) 
//	{
//		std::cerr << "Exception Occured: " << exception.GetString();
//
//		m_pst->RollbackTransaction();
//		std::cerr << "Rollback was executed!" << std::endl;
//		std::cerr << "This is our own catch() block!" << std::endl;
//	}
//
//
//	return true;
//}

//
//
//
//
//
////
//bool DB::InitName()
//{
//	if (m_pbar == NULL)
//	{
//		printf("[InitName] m_pbar NULL\n");
//		return false;
//	}
//
//
//	char* inst = m_pbar->m_serinfo.barinfo.instru;
//	char* bartype = NULL;
//	if (Bartype::tick == m_pbar->m_serinfo.barinfo.type)
//		bartype = "tick"; 
//	else if (Bartype::sec == m_pbar->m_serinfo.barinfo.type)
//		bartype = "sec"; 
//	else if (Bartype::min == m_pbar->m_serinfo.barinfo.type)
//		bartype = "min";
//	else if (Bartype::day == m_pbar->m_serinfo.barinfo.type)
//		bartype = "day";
//
//	sprintf(m_dbname, "%s/%s/%s_%s.%s", inst, bartype, inst, gTradingDay, bartype);
//
//
//	if (Bartype::tick == m_pbar->m_serinfo.barinfo.type)
//		sprintf(m_tbname, "ticktable");
//	else if (Bartype::sec == m_pbar->m_serinfo.barinfo.type)
//		sprintf(m_tbname, "sectable");
//	else if (Bartype::min == m_pbar->m_serinfo.barinfo.type)
//		sprintf(m_tbname, "mintable");
//	else if (Bartype::day == m_pbar->m_serinfo.barinfo.type)
//		sprintf(m_tbname, "daytable");
//
//
//	return true;
//}
//
//
//
//
//bool DB::InitBuffer()
//{
//	m_buffer = new char[1024];
//	if (NULL == m_buffer)
//	{
//		printf("[InitBuffer] m_buffer NULL\n");
//		return false;
//	}
//	return true;
//
//}
//
//
//DB::DB(BarSeries* pbar):m_pbar(pbar)
//{
//	if (false == InitName())
//	{
//		printf("[DB] InitName failed\n");
//		return;
//	}
//
//	if (false == InitBuffer())
//	{
//		printf("[DB] InitBuffer failed\n");
//		return;
//	}
//
//	if (false == CheckDBFile())
//	{
//		printf("[DB] CheckDBFile failed\n");
//		return;
//	}
//
//	try
//	{
//		/**********************Open DB********************************************/
//
//		m_pdb = new Kompex::SQLiteDatabase(m_dbname, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
//		// create statement instance for sql queries/statements
//		m_pst = new Kompex::SQLiteStatement(m_pdb);
//
//		cout << "SQLite version: " << m_pdb->GetLibVersionNumber() << std::endl;
//
//		/************************************************************************/
//
//
//		/**********************Create Table*****************************************/
//		memset(m_buffer, 0, sizeof(m_buffer));
//		sprintf(m_buffer, "select name from sqlite_master where type='table' and name = '%s';", m_tbname);
//
//		const char *tbName = (const char*)m_pst->GetSqlResultCString(m_buffer);
//
//		if (NULL == tbName)//table note exist, create it
//		{
//			memset(m_buffer, 0, sizeof(m_buffer));
//			sprintf(m_buffer, "CREATE TABLE %s (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);", 
//				m_tbname, "id INTEGER NOT NULL PRIMARY KEY", "starttime VARCHAR(20)", "endtime VARCHAR(20)", "tickcnt INTEGER",
//				"open DOUBLE", "close DOUBLE", "high DOUBLE", "low DOUBLE", "vol INTEGER", "turnover DOUBLE", "openinterest DOUBLE", "openinterestdiff DOUBLE",
//				"bidprice1 DOUBLE", "bidvol1 INTEGER", "bidprice2 DOUBLE", "bidvol2 INTEGER", "bidprice3 DOUBLE", "bidvol3 INTEGER", "bidprice4 DOUBLE", "bidvol4 INTEGER", "bidprice5 DOUBLE", "bidvol5 INTEGER",
//				"askprice1 DOUBLE", "askvol1 INTEGER", "askprice2 DOUBLE", "askvol2 INTEGER", "askprice3 DOUBLE", "askvol3 INTEGER", "askprice4 DOUBLE", "askvol4 INTEGER", "askprice5 DOUBLE", "askvol5 INTEGER");
//
//			//printf(m_buffer);
//
//			m_pst->SqlStatement(m_buffer);
//
//
//			m_pst->SqlStatement("PRAGMA synchronous=OFF;");
//		}
//		else//exist talbe only for test
//		{
//			SetConsoleColor(FOREGROUND_GREEN);  
//			printf("[DB] exist table:%s", m_tbname);
//			SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); 
//
//		}
//
//		/************************************************************************/
//
//
//		return;
//	}
//	catch(Kompex::SQLiteException &exception)
//	{
//		std::cerr << "\nException Occured" << std::endl;
//		exception.Show();
//
//	}
//
//};

//
//void InitColName()
//{
//	m_buffer = new char[1024];
//	m_id = new char[50];
//	m_starttime = new char[50];
//	m_endtime = new char[50];
//	m_tickcnt = new char[50];
//	m_open = new char[50];
//	m_close = new char[50];
//	m_high = new char[50];
//	m_low = new char[50];
//	m_vol = new char[50];
//	m_turnover = new char[50];
//	m_OpenInterest = new char[50];
//	m_OpenInterestDiff = new char[50];
//	m_BidPrice1 = new char[50];
//	m_BidPrice2 = new char[50];
//	m_BidPrice3 = new char[50];
//	m_BidPrice4 = new char[50];
//	m_BidPrice5 = new char[50];
//	m_BidVolume1 = new char[50];
//	m_BidVolume2 = new char[50];
//	m_BidVolume3 = new char[50];
//	m_BidVolume4 = new char[50];
//	m_BidVolume5 = new char[50];
//	m_AskPrice1 = new char[50];
//	m_AskPrice2 = new char[50];
//	m_AskPrice3 = new char[50];
//	m_AskPrice4 = new char[50];
//	m_AskPrice5 = new char[50];
//	m_AskVolume1 = new char[50];
//	m_AskVolume2 = new char[50];
//	m_AskVolume3 = new char[50];
//	m_AskVolume4 = new char[50];
//	m_AskVolume5 = new char[50];
//
//
//	sprintf(m_id, "id INTEGER NOT NULL PRIMARY KEY");
//	sprintf(m_starttime, "starttime VARCHAR(20) NOT NULL");
//	sprintf(m_endtime, "endtime VARCHAR(20) NOT NULL");
//	sprintf(m_tickcnt, "tickcnt INTEGER");
//	sprintf(m_open, "open DOUBLE");
//	sprintf(m_close, "close DOUBLE");
//	sprintf(m_high, "high DOUBLE");
//	sprintf(m_low, "low DOUBLE");
//	sprintf(m_vol, "vol INTEGER");
//	sprintf(m_turnover, "turnover DOUBLE");
//	sprintf(m_OpenInterest, "openinterest DOUBLE");
//	sprintf(m_OpenInterestDiff, "openinterestdiff DOUBLE");
//	sprintf(m_BidPrice1, "bidprice1 DOUBLE");
//	sprintf(m_BidVolume1, "bidvol1 INTEGER");
//	sprintf(m_BidPrice2, "bidprice2 DOUBLE");
//	sprintf(m_BidVolume2, "bidvol2 INTEGER");
//	sprintf(m_BidPrice3, "bidprice3 DOUBLE");
//	sprintf(m_BidVolume3, "bidvol3 INTEGER");
//	sprintf(m_BidPrice4, "bidprice4 DOUBLE");
//	sprintf(m_BidVolume4, "bidvol4 INTEGER");
//	sprintf(m_BidPrice5, "bidprice5 DOUBLE");
//	sprintf(m_BidVolume5, "bidvol5 INTEGER");
//	sprintf(m_AskPrice1, "askprice1 DOUBLE");
//	sprintf(m_AskVolume1, "askvol1 INTEGER");
//	sprintf(m_AskPrice2, "askprice2 DOUBLE");
//	sprintf(m_AskVolume2, "askvol2 INTEGER");
//	sprintf(m_AskPrice3, "askprice3 DOUBLE");
//	sprintf(m_AskVolume3, "askvol3 INTEGER");
//	sprintf(m_AskPrice4, "askprice4 DOUBLE");
//	sprintf(m_AskVolume4, "askvol4 INTEGER");
//	sprintf(m_AskPrice5, "askprice5 DOUBLE");
//	sprintf(m_AskVolume5, "askvol5 INTEGER");
//
//}
//
